import numpy as np
import matplotlib.pyplot as plt

from lagrange_interpolation import lagrange
from linear_interpolation import linear
from spline_interpolation import Spline

# Lagrange 插值
x0 = np.linspace(-1, 1, 10001)

y0 = 1 / (1 + 25 * x0 ** 2)
y1 = lagrange(x0, 10)
y2 = lagrange(x0, 15)

plt.plot(x0, y0, 'b', x0, y1, 'r', x0, y2, 'g')
plt.savefig("la1.pdf")
plt.show()

y3 = lagrange(x0, 25)
plt.plot(x0, y0, 'b', x0, y3, 'r')
plt.savefig("la2.pdf")
plt.show()

# 线性插值
y4 = linear(x0, 11)
y5 = linear(x0, 21)
y6 = linear(x0, 51)

plt.plot(x0, y0, 'b', x0, y4, 'r')
plt.savefig("l1.pdf")
plt.show()
plt.plot(x0, y0, 'b', x0, y5, 'r')
plt.savefig("l2.pdf")
plt.show()
plt.plot(x0, y0, 'b', x0, y6, 'r')
plt.savefig("l3.pdf")
plt.show()

# 自然边界的 spline 线性插值
sp1 = Spline(10)
sp2 = Spline(20)
sp3 = Spline(50)
y7 = sp1.compute_array(x0)
y8 = sp2.compute_array(x0)
y9 = sp3.compute_array(x0)
plt.plot(x0, y0, 'b', x0, y7, 'r')
plt.savefig("s1.pdf")
plt.show()
plt.plot(x0, y0, 'b', x0, y8, 'r')
plt.savefig("s2.pdf")
plt.show()
plt.plot(x0, y0, 'b', x0, y9, 'r')
plt.savefig("s3.pdf")
plt.show()

# 看看误差多大

print(np.max(abs(y0 - y1)))
print(np.max(abs(y0 - y2)))
print(np.max(abs(y0 - y3)))
print(np.max(abs(y0 - y4)))
print(np.max(abs(y0 - y5)))
print(np.max(abs(y0 - y6)))
print(np.max(abs(y0 - y7)))
print(np.max(abs(y0 - y8)))
print(np.max(abs(y0 - y9)))
